package com.uls.ldcm.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.uls.framework.config.MinioProp;
import com.uls.framework.utils.MinioUtils;
import com.uls.ldcm.domain.Contracts;
import com.uls.ldcm.service.IContractsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.ldcm.domain.Laborers;
import com.uls.ldcm.service.ILaborersService;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 基本信息Controller
 * 
 * @author gw
 * @date 2024-10-31
 */
@RestController
@RequestMapping("/ldcm/laborers")
public class LaborersController extends BaseController
{
    @Autowired
    private ILaborersService laborersService;

    @Autowired
    private IContractsService contractsService;


    @Autowired
    private MinioUtils minioUtils;


    @Autowired
    private MinioProp minioProp;

    /**
     * 查询基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('ldcm:laborers:list')")
    @GetMapping("/list")
    public TableDataInfo list(Laborers laborers)
    {
        startPage();
        List<Laborers> list = laborersService.selectLaborersList(laborers);
        for(Laborers laborers1:list){
            Contracts contracts = contractsService.selectContractsByContractId(laborers1.getContractId());
            laborers1.setContractTerms(contracts.getContractTerms());
        }
        return getDataTable(list);
    }

    /**
     * 导出基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('ldcm:laborers:export')")
    @Log(title = "基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Laborers laborers)
    {
        List<Laborers> list = laborersService.selectLaborersList(laborers);
        ExcelUtil<Laborers> util = new ExcelUtil<Laborers>(Laborers.class);
        util.exportExcel(response, list, "基本信息数据");
    }

    /**
     * 获取基本信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('ldcm:laborers:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(laborersService.selectLaborersById(id));
    }

    /**
     * 新增基本信息
     */
    @PreAuthorize("@ss.hasPermi('ldcm:laborers:add')")
    @Log(title = "基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Laborers laborers)
    {
        return toAjax(laborersService.insertLaborers(laborers));
    }

    /**
     * 修改基本信息
     */
    @PreAuthorize("@ss.hasPermi('ldcm:laborers:edit')")
    @Log(title = "基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Laborers laborers)
    {
        return toAjax(laborersService.updateLaborers(laborers));
    }

    /**
     * 删除基本信息
     */
    @PreAuthorize("@ss.hasPermi('ldcm:laborers:remove')")
    @Log(title = "基本信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(laborersService.deleteLaborersByIds(ids));
    }

    /**
     * 上传头像图片
     * @param file
     * @param request
     * @return
     */
    @PostMapping("/upload")
    public String upload(@RequestParam(name = "file", required = false) MultipartFile file, HttpServletRequest request) {
        JSONObject res = null;
        try {
            res = minioUtils.uploadFile(file, minioProp.getBucket());
        } catch (Exception e) {
            e.printStackTrace();
            res.put("code", 0);
            res.put("msg", "上传失败");
        }
        return res.toJSONString();
    }
}
